package com.sc.nft.dao;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.DaoSchoolArticleMaterial;
import com.sc.nft.entity.DaoSchoolMaterial;
import com.sc.nft.entity.dto.DaoSchoolArticleMaterialSaveAdminDTO;
import com.sc.nft.entity.dto.DaoSchoolMaterialArticlePageAdminDTO;
import com.sc.nft.entity.vo.ArticleMaterialVO;
import com.sc.nft.entity.vo.DaoSchoolMaterialArticlePageAdminVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.DaoSchoolArticleMaterialMapper;
import com.sc.nft.pool.SysMemberPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 素材表与文章表关联表(DaoSchoolArticleMaterial)表Dao层
 *
 * @author makejava
 * @since 2023-03-13 16:21:33
 */
@Slf4j
@Repository
public class DaoSchoolArticleMaterialDao extends ServiceImpl<DaoSchoolArticleMaterialMapper, DaoSchoolArticleMaterial> {

    @Resource
    private DaoSchoolMaterialDao daoSchoolMaterialDao;

    public Boolean deleteByArticleId(Long id) {
        QueryWrapper<DaoSchoolArticleMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", id);
        baseMapper.delete(queryWrapper);
        return Boolean.TRUE;
    }

    /**
     * 后台获取分页列表
     *
     * @param daoSchoolMaterialArticlePageAdminDTO
     * @return
     */
    public Page<DaoSchoolMaterialArticlePageAdminVO> pageAdmin(DaoSchoolMaterialArticlePageAdminDTO daoSchoolMaterialArticlePageAdminDTO) {
        return baseMapper.pageAdmin(daoSchoolMaterialArticlePageAdminDTO.toMp(), daoSchoolMaterialArticlePageAdminDTO);
    }

    /**
     * 后台获取分页列表
     *
     * @param daoSchoolMaterialArticlePageAdminDTO
     * @return
     */
    public Page<DaoSchoolMaterialArticlePageAdminVO> pageNotAdmin(DaoSchoolMaterialArticlePageAdminDTO daoSchoolMaterialArticlePageAdminDTO) {
        return baseMapper.pageNotAdmin(daoSchoolMaterialArticlePageAdminDTO.toMp(), daoSchoolMaterialArticlePageAdminDTO);
    }

    /**
     * 删除附件
     *
     * @param id
     * @return
     */
    public Boolean deleteById(Long id) {
        return SqlHelper.retBool(baseMapper.deleteById(id));
    }

    /**
     * 新增附件
     *
     * @param listInfo
     * @return
     */
    public Boolean saveAdmin(List<DaoSchoolArticleMaterialSaveAdminDTO> listInfo) {
        if (CollUtil.isEmpty(listInfo)) {
            throw new GlobalRunTimeException("请求参数不能为空");
        }
        List<DaoSchoolArticleMaterial> list = new ArrayList<>();
        listInfo.forEach(daoSchoolArticleMaterialSaveAdminDTO -> {
            DaoSchoolMaterial daoSchoolMaterial = daoSchoolMaterialDao.getById(daoSchoolArticleMaterialSaveAdminDTO.getMaterialId());
            if (BeanUtil.isEmpty(daoSchoolMaterial)) {
                throw new GlobalRunTimeException("素材有未存在的数据");
            }
            if (selectByArticleIdAndMaterialId(daoSchoolArticleMaterialSaveAdminDTO.getArticleId(), daoSchoolArticleMaterialSaveAdminDTO.getMaterialId())) {
                return;
            }
            DaoSchoolArticleMaterial daoSchoolArticleMaterial = new DaoSchoolArticleMaterial();
            BeanUtil.copyProperties(daoSchoolArticleMaterialSaveAdminDTO, daoSchoolArticleMaterial);
            daoSchoolArticleMaterial.setCreateId(SysMemberPool.getMember().getId());
            daoSchoolArticleMaterial.setModifyId(SysMemberPool.getMember().getId());
            daoSchoolArticleMaterial.setModifyTime(DateUtil.date());
            list.add(daoSchoolArticleMaterial);
        });
        if (CollUtil.isEmpty(list)) {
            throw new GlobalRunTimeException("程序执行异常");
        }
        Integer saveBatch = baseMapper.saveBatch(list);
        return SqlHelper.retBool(saveBatch);
    }

    public Boolean selectByArticleIdAndMaterialId(Long article, Long materialId) {
        QueryWrapper<DaoSchoolArticleMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", article);
        queryWrapper.eq("material_id", materialId);
        Integer selectCount = baseMapper.selectCount(queryWrapper);
        return SqlHelper.retBool(selectCount);
    }


    public Integer selectByArticleIdSum(Long article) {
        QueryWrapper<DaoSchoolArticleMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", article);
        Integer selectCount = baseMapper.selectCount(queryWrapper);
        return selectCount;
    }

    /**
     * 根据文章id获取关联数据
     *
     * @param id
     * @return
     */
    public List<ArticleMaterialVO> selectByArticleId(Long id) {
        return baseMapper.selectByArticleId(id);
    }
}
